package com.example.user.service;



import com.example.user.dao.ProductMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.PageParam;
import entity.Rsp;
import entity.Product;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public PageInfo<Product> searchSomeByPage(PageParam<Product> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Product> productList=productMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Product> productPageInfo = new PageInfo<Product>(productList);

            return productPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Product> productList=productMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Product> productPageInfo = new PageInfo<Product>(productList);

            return productPageInfo;

        }

    }

    @Override
    public Product getOneById(String id) {
        return productMapper.getOneById(id);
    }

    @Override
    public Rsp addOne(Product product) {

        product.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//        product.setCreateTime(new Date());



        return productMapper.addOne(product)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp deleteOneTrue(String id) {



        return productMapper.deleteOneTrue(id)==1 ? Rsp.success(null).msg("物理删除成功"):Rsp.fail().msg("物理删除失败");
    }

    @Override
    public Rsp update(Product product) {


        return productMapper.update(product)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteLogical(Product product) {

        product.setIsDeleted(1);

        return productMapper.update(product)==1?Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    @Override
    public Rsp recoverLogical(Product product) {

        product.setIsDeleted(0);

        return productMapper.update(product)==1?Rsp.success(null).msg("逻辑恢复成功"):Rsp.fail().msg("逻辑恢复失败");
    }

    @Override
    public Rsp deleteBatchTrue(List<String> ids) {


        return productMapper.deleteBatchTrue(ids)==ids.size()?Rsp.success(null).msg("批量物理删除成功"):Rsp.fail().msg("批量物理删除失败");
    }

    @Override
    public Rsp deleteBatchLogical(List<String> ids) {


        return productMapper.deleteBatchLogical(ids)==ids.size()?Rsp.success(null).msg("批量物理删除成功"):Rsp.fail().msg("批量物理删除失败");
    }

    @Override
    public Rsp deleteSomeLogicalByCondition(Product product) {
        return null;
    }

    @Override
    public Rsp deleteSomeTrueByCondition(Product product) {
        return null;
    }


}
